<?php

namespace App\Console\Commands;

use App\Enums\LogType;
use App\Enums\Status;
use App\Mail\Seiterestore;
use App\Mail\Sitefail;
use App\Mail\Siterestore;
use App\Models\Log;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;

class Site extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'site';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        \App\Models\Site::with('notice_template')->orderBy('time', 'asc')->chunk('5', function ($sites) {

            /**
             * @var $site \App\Models\Site
             */
            $client = new Client();
            foreach ($sites as $site) {
                $status = Status::COMMON;
                $failure = false;
                //判断频率
                if(strtotime($site->next_perform_time) > time()){
                    $site->time = date('Y-m-d H:i:s');
                    continue;
                }
                $site->next_perform_time = date('Y-m-d H:i:s',time()+$site->notice_template->frequency *60);
                try {
                    $res = $client->request($site->method, $site->web_site_url ,['connect_timeout' => 2]);
                    $body = $res->getBody();
                    $code = $res->getStatusCode();
                    if ($code != '200') {
                        $failure = true;
                    }
                } catch (RequestException $e) {
                    $status = Status::NetFail;
                    $body = '网络请求失败';
                    $failure = true;
                }

                if ($failure == true ) {
                    $site->error_num ++;
                    if($site->error_num == $site->notice_template->greater_num_stop){
                        //发送有错误
                        Mail::to($site->notice_template->email)->send(new Sitefail($site));
                    }
                }
                if($failure == false && $site->error_num != 0){
                    //网站恢复了
                    if($site->error_num >$site->notice_template->greater_num_stop){
                        Mail::to($site->notice_template->email)->send(new Siterestore($site));
                    }
                    $site->error_num =0;

                }
                $site->time = date('Y-m-d H:i:s');
                $site->save();
//                Log::create([
//                    'type' => LogType::create,
//                    'log' => $body,
//                    'user_id' => $site->admin_user_id,//该网站的拥有者
//                    'status' => $status
//                ]);
            }
        });
    }
}
